my $infile = $ARGV[0];
#my $infile = "r49.constraints";

open(FD, "constraints/$infile");
my @all = <FD>;
close(FD);

my $ntotalvars = 0;
my $ntotalconstraints = 0;
my %vartoid = ();
my $gnextid = 0;

sub getnewid {
	return $gnextid++;
}
sub getcode {
	my ($left, $right) = @_;
	$leftval = "";
	$rightval = "";

	if ($left =~ /\*/) {
		$left =~ s/\*//;
		$leftval .= "$left";
		$rightval .= "-1";
	} else {
		$leftval .= $left;
	}
	if ($right =~ /\*/) {
		$right =~ s/\*//;
		$leftval .= "-$right";
		$rightval .= "+1";
	} elsif ($right =~ /\&/) {
		$right =~ s/\&//;
		$leftval .= "-$right";
		$rightval .= "-1";
	} else {
		$leftval .= "-$right";
		$rightval .= "0";
	}

	return "$leftval >= $rightval";
}
sub makeproper {
	my ($str) = @_;
	$str =~ s/:/colon/g;
	$str =~ s/</less/g;
	$str =~ s/>/more/g;
	$str =~ s/\./dot/g;
	$str =~ s/\(/popen/g;
	$str =~ s/\)/pclose/g;
	$str =~ s/_/us/g;
	$str =~ s/theta/thetaX/g;
	return $str;
}
my $outfile = $infile;
$outfile =~ s/constraints/ilog/;

open(FD, ">ilog/$outfile");
print FD "Minimize x\nSubject To\n";
for (my $ii = 0; $ii < scalar @all; ++$ii) {
	#print $all[$ii];
	$all[$ii] =~ /(.*)=(.*)/;
	$left = $1; $right = $2;
	$left = makeproper($left); $right = makeproper($right);
	#print "$left===$right";
	print FD getcode($left, $right)."\n";
}
print FD "End\n";
close(FD);

#system("cat $outfile");
